package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.ProductDetailsMapper;
import com.zhentao.mapper.ProductSkuMapper;
import com.zhentao.model.entity.product.ProductDetails;
import com.zhentao.model.entity.product.ProductSku;
import com.zhentao.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductSkuServiceImpl implements ProductSkuService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public ProductSku findById( Long id ) {
        ProductSku productSku = productSkuMapper.selectOne( Wrappers.< ProductSku > lambdaQuery()
                .eq( ProductSku::getProductId, id )
                .last( "limit 1" ) );
        ProductDetails productDetails = productDetailsMapper.selectOne( Wrappers.< ProductDetails > lambdaQuery()
                .eq( ProductDetails::getProductId, id ) );
        productSku.setImageUrls( productDetails.getImageUrls() );
        return productSku;
    }

    @Override
    public List< ProductSku > findAllById( Long id ) {
        List< ProductSku > list = productSkuMapper.selectList( Wrappers.< ProductSku > lambdaQuery()
                .eq( ProductSku::getProductId, id ) );
        for (ProductSku productSku : list) {
            ProductDetails productDetails = productDetailsMapper.selectOne( Wrappers.< ProductDetails > lambdaQuery()
                    .eq( ProductDetails::getProductId, id ) );
            productSku.setImageUrls( productDetails.getImageUrls() );
        }
        return list;
    }
}
